package br.com.galluzzo;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

@Path("getRDFXML")
public class GetRDFasXML {
	
	@GET
	public String getRDF(@QueryParam("url") String url) throws IOException {
		URL u;
		Model model = ModelFactory.createDefaultModel();
		ByteArrayOutputStream os = null;
		try {
			u = new URL(url+"?output=rdfxml");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					u.openStream()));
			
			model.read(br,u.getHost().concat(u.getPath()));
			String output;
			
			os = new ByteArrayOutputStream();
			model.write(os);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String out = new  String(os.toByteArray());
		return out ;
		
	}
}
